<?php

class FormulaireHelper{
	
	private $listeElements = null;
	
	private $pageConfig = null;
	private $idForm = null;
	private $isPost = null;
	private $method = null;
	private $action = null;
	private $enctype = null;
	private $listErrors = null;
	
	public function __construct($id, $method, $action, $enctype){

		session_start();

		$this->idForm = $id;
		$this->listErrors = $_SESSION[$this->getSeedIdElement('sessionListError')];
		
		$this->method = $method;
		
		if($this->method === 'POST'){
			$this->isPost = true;
		}else{
			$this->isPost = false;
		}
		
		$this->action = $action;
		$this->enctype = $enctype;
		
		$this->listeElements = array();
		
	}
	
	public function check(){

		$listErrors = array();
		
		foreach($this->listeElements as $element){
			
			$result = null;
			$idElementTmp = $element->getId();
			
			if($this->isPost){
				$result = $element->check($_POST[$idElementTmp]);
			}else{
				$result = $element->check($_GET[$idElementTmp]);
			}
			
			if(!empty($result)){
				$listErrors[$idElementTmp] = $result;
			}
		}
		
		$this->listErrors = $listErrors;
		$_SESSION[$this->getSeedIdElement('sessionListError')] = $this->listErrors;
		
		return $this->listErrors;
	}
	
	private function getSeedIdElement($idElement){
		
		return $this->idForm . '_' . $idElement;
	}
	
	public function addElement($element){
		
		$idElement = $element->getId();
		
		$element->setId( $this->getSeedIdElement($idElement) );
				
		$this->listeElements[$idElement] = $element;
	}
	
	public function generate($idElement){
		
		$this->listeElements[$idElement]->generate();
	}
	
	public function generateLabel($idElement){
		
		$element = $this->listeElements[$idElement];
		
		if($element->getLabel() != null){
			echo '<label class="labelSimpleHelper" for="'.$element->getId().'">'.$element->getLabel().'</label>';
		}
	}
	
	public function generateError($idElement){
		
		$listErrorsElement = (array) $this->listErrors[$this->getSeedIdElement($idElement)];

		foreach($listErrorsElement as $ErrorElement){
			echo $ErrorElement . '<br />';
		}
	}
	
	public function generateDebutForm(){
		
		echo '<form id="'.$this->idForm.'" name="'.$this->idForm.'" method="'.$this->method.'" action="'.$this->action.'" enctype="'.$this->enctype.'">';
	}
	
	public function generateFinForm(){
		
		echo '<input type="hidden"  name="formCibleInitiale"  value="'.$_SERVER['PHP_SELF'].'">';
		echo '</form>';
	}
	
	
	
	public function getValue($idElement){
	
		if( ($this->isPost) && (isset($_POST[$idElement])) ){
				
			return $_POST[$idElement];
				
		}else if( isset($_GET[$idElement]) ){
				
			return $_GET[$idElement];
				
		}else{
				
			return null;
				
		}
	
	}
	
	public function hasError(){
		
		return !empty($this->listErrors);
	}
	
	public function dataInvalid(){
		
		header('Location: ' . $this->getValue(formCibleInitiale));
	}

	
}